from datetime import datetime
from openai import OpenAI
import json
import os
from typing import List, Dict, Any, Optional

class OpenAIClient:
    """OpenAI兼容客户端封装"""

    def __init__(self, model: str = "qwen3:4b"):
        self.client = self._create_client()
        self.model = model

    def _create_client(self) -> OpenAI:
        """创建客户端实例"""
        return OpenAI(
            api_key=os.getenv("OLLAMA_API_KEY", "ollama"),
            base_url=os.getenv("OLLAMA_BASE_URL", "http://localhost:11434/v1"),
        )

    def chat_completion(self, messages: List[Dict[str, Any]], tools: List[Dict[str, Any]]) -> Any:
        """发起聊天完成请求"""
        return self.client.chat.completions.create(
            model=self.model,
            messages=messages,
            tools=tools,
            tool_choice="auto"
        )
